<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Money extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('MoneyModel','mm'); //加载model模型类
        $this->load->helper('cookie');
    }
    public function home(){
        $this->load->View('money/money.html'); //传递数据到view中
    }

    function index(){

                   $data=$this->input->post();//接收传过来的值
                   $cookie=$_COOKIE['phone'];//提取cookie里面的phone
                   $time=time();//添加时间戳
                   $a= $this->db->where('phone',$cookie)->select('fid')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                   if($a['fid']==0){return ;}  //寻找有没有上级如果没有就return出去，如果有的话就继续向下执行

          //这是第一级分销
                //取出自己的信息
                $one=$this->db->where('relationship.id',$a['fid'])->select('money,relationship.id,phone,fid')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                //计算上一级充值的利率
                $f=$data['money'] * 0.1;
                //余额和利率相加
                $u=$f+$one['money'];
                $date=array(
                    'money'=>$u
                );
                //将金额修改相加的金额
                $this->db->update('relationship',$date,array('id'=>$one['id']));
                // echo $this->db->last_query();die();//打印sql语句
                //添加金额来源
                $co=array(
                    'relationship_id'=>$one['id'],
                    'contect'=>'来自下线'.$cookie.'利率'.$f.'元',
                    'create_time'=>$time,
                );
                $this->db->insert('contect',$co);
                // echo $this->db->last_query();die();
                  if($one['fid']==0){return ;}  //寻找有没有上级如果没有就return出去，如果有的话就继续向下执行

         //这是第二级分销
                //取出自己现有的余额
                $two=$this->db->where('relationship.id',$one['fid'])->select('money,relationship.id,phone,fid')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                //计算上一级充值的利率
                $ff=$data['money'] * 0.06;
                //余额和利率相加
                $uu=$ff+$two['money'];
                $date=array(
                    'money'=>$uu
                );
                //将金额修改相加的金额
                $this->db->update('relationship',$date,array('id'=>$two['id']));
                // echo $this->db->last_query();die();//打印sql语句
                //添加金额来源
                $con=array(
                    'relationship_id'=>$two['id'],
                    'contect'=>'来自下线'.$one['phone'].'的下线'.$cookie.'利率'.$ff.'元',
                    'create_time'=>$time,
                );
                $this->db->insert('contect',$con);
                if($two['fid']==0){return ;} //寻找有没有上级如果没有就return出去，如果有的话就继续向下执行

        //这是第三级分销
                //取出自己现有的余额
                $three=$this->db->where('relationship.id',$two['fid'])->select('money,relationship.id,phone,fid')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                //计算上一级充值的利率
                $fff=$data['money'] * 0.03;
                //余额和利率相加
                $uuu=$fff+$three['money'];
                $date=array(
                    'money'=>$uuu
                );
                //将金额修改相加的金额
                $this->db->update('relationship',$date,array('id'=>$three['id']));
                // echo $this->db->last_query();die();//打印sql语句
                //添加金额来源
                $co=array(
                    'relationship_id'=>$three['id'],
                    'contect'=>'来自下线'.$two['phone'].'的下线'.$one['phone'].'的下线'.$cookie.'利率'.$fff.'元',
                    'create_time'=>$time,
                );
                $this->db->insert('contect',$co);
                if($three['fid']==0){return ;}  //寻找有没有上级如果没有就return出去，如果有的话就继续向下执行
    }

}